<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%@ page import="java.util.*"%>
<%@ page import="forum.*"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";

	String username = (String) request.getSession().getAttribute(
			"username");
	Boolean isModerator = (Boolean) request.getSession().getAttribute(
			"isModerator");
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script type="text/javascript">
	var int = self.setInterval("checkNotifications()", 10000);

	function checkNotifications() {
		check();
	}
	
	function check(){
		var xmlhttp;
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.open("GET", "viewPostsProcessor?do=check", true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					var friend = xmlhttp.responseXML.getElementsByTagName("response")[0].getElementsByTagName("friend")[0].firstChild.data;
					if (friend == "y")
						alert("Your friend added post");
					var author = xmlhttp.responseXML.getElementsByTagName("response")[0].getElementsByTagName("author")[0].firstChild.data;
					if (author == "y")
						alert("Post added to your thread");
					var viewer = xmlhttp.responseXML.getElementsByTagName("response")[0].getElementsByTagName("viewer")[0].firstChild.data;
					if (viewer != "n"){
						var divtag = document.createElement("div");
						divtag.align = "center";
						divtag.innerHTML = viewer;
						document.getElementById("posts").appendChild(divtag);
					}
				} else
					alert("Error\n" + xmlhttp.status + ":" + xmlhttp.statusText);
			}
		};
		xmlhttp.send(null);
	}
	
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<base href="<%=basePath%>">

<title>Posts list</title>
</head>
<body background="background.jpg" bgcolor="AliceBlue" link="Navy" text="Navy"  >
<font face="COMIC SANS MS" >

	<h1 align="center">
		<font size="10"  color="FloralWhite">Posts list</font>
	</h1>

	<%
			List<Post> postsList = (ArrayList) request.getAttribute("posts");
			int threadID = ((Integer) request.getAttribute("threadID"))
					.intValue();
			
			for (Post p : postsList) {
		%>
	
	<hr>
<p align="center">
	<br> #<%=p.getID()%>.
	<%
 	if (p.getParent() != null)
 			out.println("<u>Reply to post #" + p.getParent().getID()
 					+ "</u>");
 %>
	<br>
	<b><%=p.getAuthor()%></b> posted at
	<b><%=p.getDate()%></b>
	<br>
	<font size="5" ><b>Subject: </b><%=p.getSubject()%>
	</br></font>
	<font size="5" ><b>Content: </b><%=p.getContent()%>
	<br></font>
	<%
				if (p.getAuthor().equalsIgnoreCase(username))
						out.println("&nbsp &nbsp &nbsp <a href=\"viewPostsProcessor?do=edit&id="
								+ p.getID()
								+ "&author="
								+ p.getAuthor()
								+ "\">Edit</a>");
					if (p.getAuthor().equalsIgnoreCase(username)
							|| (isModerator != null && isModerator.booleanValue()))
						out.println("&nbsp &nbsp &nbsp <a href=\"viewPostsProcessor?do=delete&id="
								+ p.getID()
								+ "&author="
								+ p.getAuthor()
								+ "\">Delete</a>");
			%>
	</p>
	<%
			} //for
		%>
	
	<div id="posts"></div>
	
	<br>
	<hr>
	<br>
	<p align="center">
	<%
		out.println("<font size =\"5\"><a href=\"viewPostsProcessor?do=reply&id=" + threadID
				+ "\">Add post</a></font>");
	%>
	<br>
	<br>
	<font color="Navy" size ="5">
		<a href="viewFriends">Show friends</a> <br> <a href="viewForums">Back
			to Forums</a> <br> <a href="logout">logout</a>
	</font>
	</p>
</font>
</body>
</html>